package com.goal98.team.domain

import com.goal98.grails.plugin.account.domain.Account
import com.goal98.team.Constants

/**
 * User domain class.
 */
class Person {

    static searchable = {
        analyzer 'chinese'
        position component: true
        team component: true
    }

	static transients = ['pass', 'teamAdmin']
	static hasMany = [authorities: Authority,activityAttend:Activity]
	static belongsTo = Authority

    Team team
    Account account

    Position position
    String handPhone
    BigDecimal height
    BigDecimal weight

	/** Username */
	String username
	/** User Real Name*/
	String userRealName
	/** MD5 Password */
	String passwd
	/** enabled */
	boolean enabled

//	String email
	boolean emailShow

    boolean receiveEmail = false

	/** description */
	String description = ''

	/** plain password to create a MD5 password */
	String pass = '[secret]'

	static constraints = {
//        email(blank: false, unique: true)
		username(blank: false, unique: true, email:true)
		userRealName(blank: false)
		passwd(blank: true, nullable:true)
		enabled()
        team(nullable:true)
        account(nullable:true)
        description(blank:true)
        position(nullable:true)
        handPhone(blank:true, nullable:true)
        height(blank:true, nullable:true)
        weight(blank:true, nullable:true)
	}

    boolean isTeamAdmin(){
        boolean result = false
        authorities?.each{Authority authority->
            if(authority.authority == Constants.ROLE_TEAM_ADMIN)
                result = true
        }
        return result
    }

    String toString(){
        return userRealName
    }

    static mapping = {
        cache true
        authorities cache:true, include:'non-lazy'
        position cache:true
        team cache:true
        activityAttend cache:true, include:'non-lazy'
        account cache:true
    }

}
